package com.example.day03_32.base;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.day03_32.util.ToastUtil;
import com.example.day03_32.widget.LoadingDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment <P extends BasePresenter> extends Fragment
        implements BaseView {

    private Unbinder unbinder;
    protected P mPresenter;
    private LoadingDialog mLoadingDialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(getLayout(), null);
            unbinder = ButterKnife.bind(this, inflate);
            mPresenter=initPresenter();
        if(mPresenter!=null){
            mPresenter.bindView(this);
        }
        initView(inflate);
        initData();
        initListener();
        return inflate;
    }

    protected abstract void initListener();

    protected abstract void initData();

    protected abstract void initView(View inflate);

    protected abstract P initPresenter();

    protected abstract int getLayout();


    @Override
    public void onDestroy() {
        super.onDestroy();
        //需要解绑
        unbinder.unbind();
        if(mPresenter!=null){
            mPresenter.destroy();
            mPresenter=null;
        }
        hideLoading();
    }

    @Override
    public void showToast(String msg) {
        ToastUtil.showToastLong(msg);
    }

    @Override
    public void showLoading() {
        if(mLoadingDialog==null){
            mLoadingDialog=new LoadingDialog(getContext());

        }
        mLoadingDialog.show();
    }

    @Override
    public void hideLoading() {
        if(mLoadingDialog!=null){
            mLoadingDialog.dismiss();
        }
    }
}
